<template>
	<view>
		<tui-skeleton v-if="skeletonShow" backgroundColor="#fafafa" borderRadius="10rpx"></tui-skeleton>
		<view class="container">
			<view class="tui-searchbox">
				<view class="tui-search-input" @tap="search">
					<icon type="search" :size="13" color="#999"></icon>
					<text class="tui-search-text">搜索商品</text>
				</view>
			</view>
			<scroll-view scroll-y scroll-with-animation class="tab-view" :scroll-into-view="scrollViewId"
				:style="{ height: height + 'px', top: top + 'px' }">
				<view :id="`id_${index}`" v-for="(item, index) in cates" :key="item.category_id" class="tab-bar-item"
					:class="[currentTab == index ? 'active' : '']" :data-current="index" @tap.stop="swichNav">
					<text>{{ item.category_name }}</text>
				</view>
			</scroll-view>
			<block v-for="(item, index) in cates" :key="item.category_id">
				<scroll-view scroll-y class="right-box" :style="{ height: height + 'px', top: top + 'px' }"
					@scrolltolower="handleScrolltolower" v-if="currentTab == index">
					<view class="page-view">
						<view v-if="smallproductlist.length>0">
							
						</view>
						<swiper indicator-dots autoplay circular :interval="5000" :duration="150" class="swiper" >
							<swiper-item>
					
								<image :src="item.category_pic" class="slide-image" mode="scaleToFill" />
								</swiper-item>

						</swiper>
						<view class="class-box tui-skeleton-rect">
							
								
							<!-- 	<view class="g-containers" v-if="smallproductlist.length>0">
									<view v-for="(items, index) in smallproductlist" :key="index">
									<view class="g-boxs" @click="detail(smallproductlist[index]['goods_id'])">
									
										<image :src="items.pic_cover_small" class="g-image"/>
										<view class="g-titles">{{items.goods_name}}</view>
									</view>
									</view>
									
								</view> -->
							
							<view class="class-item" v-for="(items, indexs) in item.child_list" :key="indexs">
								<view class="class-name">{{ items.category_name }}</view>
								<view class="g-container">
									<view class="g-box"  data-key="" v-for="(itemss, indexss) in items.child_list">
										<image :src="itemss.category_pic" class="g-image" @tap="productList(itemss.category_id)"/>
										<view class="g-title">{{itemss.category_name}}</view>
									</view>
									
								</view>
							</view>
							
						</view>
					</view>
					<!--内容部分 end 自定义可删除-->
				</scroll-view>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cates: '',
				height: 0, //scroll-view高度
				top: 0,
				currentTab: 0, //预设当前项的值
				scrollViewId: "id_0",
				productLists: [],
				skeletonShow: true,
				pageIndex: 1,
				total: '',
				category_id: 1,
				banner:[],
				smallproductlist:''
			};
		},
		onLoad: function(options) {
			setTimeout(() => {
				uni.getSystemInfo({
					success: res => {
						let header = 92;
						let top = 0;
						//#ifdef H5
						top = 44;
						//#endif
						this.height = res.windowHeight - uni.upx2px(header);
						this.top = top + uni.upx2px(header);
					}
				});
			}, 50);
		},
		onShow() {

			this.tui.haslogin();
			if (this.tui.isLogin()) {
				this.init();
				this.inits();
				// this.banners();
			}
		},
		methods: {
			// 点击标题切换当前页时改变样式
			swichNav: function(e) {
		        this.smallproductlist=''; 
				let cur = e.currentTarget.dataset.current;
				this.category_id = cur + 1;
				this.pageIndex=1;
				// this.inits();
                
				if (this.currentTab == cur) {
					return false;
				} else {
					this.currentTab = cur;
					this.checkCor();
				}
		
					// this.productList(this.category_id);
				this.inits();
			
			},
			init() {
				let token = this.tui.getToken();
				let datas = {
					token: token,
				};
				let cate = this.tui.request('goods', 'post', datas, 1000, 1, '', 'goodsCategory');

				Promise.resolve(cate).then((value) => {
					if (value.code == 200) {
						this.cates = value.data;
						this.skeletonShow = false
					} else {
						this.tui.toast(value.message);
					}
				})
			},
			inits() {
				this.pageIndex=1;
				let token = this.tui.getToken();
				let datas = {
					token: token,
					category_id: this.category_id,
					sort: 'asc',
					page: this.pageIndex,
					limit: 20,
				};
				let cate = this.tui.request('goods', 'post', datas, 1000, 1, '', 'goodsList');

				Promise.resolve(cate).then((value) => {
					if (value.code == 200) {
						this.smallproductlist = value.data.data
						this.total = value.data.total_count;
						this.pageIndex = this.pageIndex + 1;
			
					}
				})
			},
			handleScrolltolower() {

				let token = this.tui.getToken();
				let datas = {
					token: token,
					category_id: this.category_id,
					sort: 'asc',
					page: this.pageIndex,
					limit: 20,
				};
				let cate = this.tui.request('goods', 'post', datas, 1000, 1, '', 'goodsCategory');

				Promise.resolve(cate).then((value) => {
					if (value.code == 200) {
						if(value.data.data){
							this.productLists = this.productLists.concat(value.data.data);
							this.pageIndex = this.pageIndex + 1;
						}

					}
				})

			},
			banners:function(){
				let token = this.tui.getToken();
				//banner 
				let banner = this.tui.request('goods', 'post', {
					"token": token
				}, 1000, 1, '', 'classbanner');
				Promise.resolve(banner).then((value) => {
					if (value.code == 200) {
						this.banner = value.data.adv_list;
						this.skeletonShow = false
					} else {
						this.tui.toast(value.message);
					}
				})
			},
			//判断当前滚动超过一屏时，设置tab标题滚动条。
			checkCor: function() {
				if (this.currentTab > 6) {
					this.scrollViewId = `id_${this.currentTab - 2}`;
				} else {
					this.scrollViewId = `id_0`;
				}
			},
			detail: function(goods_id) {
				uni.navigateTo({
					url: '../productDetail/productDetail?goods_id=' + goods_id
				});
			},
			productList(e) {
				// let key = e.currentTarget.dataset.key;
				uni.navigateTo({
					url: '../productList/productList?category_id=' + e
				});
			},
			search: function() {
				uni.navigateTo({
					url: '../search/search'
				});
			}
		},

	};
</script>

<style>
	page {
		background: #f6f6f6;
	}

	/* 左侧导航布局 start*/

	/* 隐藏scroll-view滚动条*/

	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	}

	.tui-header-banner {
		box-sizing: border-box;
		padding-top: 22rpx;
		background: #fff;
	}

	.yui-sales {
		width: 100%;
		/* width:44rpx; */
	}

	.tui-searchbox {
		width: 100%;
		height: 92rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		background: #e41f19;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		left: 0;
		top: 0;
		/* #ifdef H5 */
		top: 44px;
		/* #endif */
		z-index: 100;
	}

	.tui-banner-box {
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		position: absolute;
		/* overflow: hidden; */
		z-index: 99;
		/* 	bottom: -80rpx;
		left: 0; */
	}

	.g-btns {
		width: 40rpx;
		height: 40rpx;

		position: absolute;
		bottom: 20rpx;
		right: 10rpx;
	}

	.tui-banner-swiper {
		width: 100%;
		height: 320rpx;
		border-radius: 14rpx;
		overflow: hidden;
		transform: translateY(0);
	}

	.tui-slide-image {
		width: 100%;
		height: 340rpx;
		display: block;
	}

	.g-pricebox {
		font-size: 24rpx;
		line-height: 44rpx;
		font-weight: bold;
		margin-right: 6rpx;
		;
	}

	.tui-searchbox::after {
		content: '';
		position: absolute;
		border-bottom: 1rpx solid #d2d2d2;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		bottom: 0;
		right: 0;
		left: 0;
	}

	.tui-search-input {
		width: 100%;
		height: 60rpx;
		background: #f1f1f1;
		border-radius: 30rpx;
		font-size: 26rpx;
		color: #999;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.tui-search-text {
		padding-left: 16rpx;
	}

	.tab-view {
		/* height: 100%; */
		width: 200rpx;
		position: fixed;
		left: 0;
		z-index: 10;
	}

	.tab-bar-item {
		width: 200rpx;
		height: 110rpx;
		background: #ffffff;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
	}

	.active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #f6f6f6;
	}

	.active::before {
		content: '';
		position: absolute;
		border-left: 8rpx solid #e41f19;
		height: 30rpx;
		left: 0;
	}

	/* 左侧导航布局 end*/

	.right-box {
		width: 100%;
		position: fixed;
		padding-left: 220rpx;
		box-sizing: border-box;
		left: 0;
	}

	.page-view {
		width: 100%;
		overflow: hidden;
		padding-top: 20rpx;
		
		padding-right: 20rpx;
		box-sizing: border-box;
		padding-bottom: env(safe-area-inset-bottom);
	}

	.swiper {
		width: 100%;
		height: 220rpx;
		margin-right: 20rpx;
		border-radius: 12rpx;
		overflow: hidden;
		transform: translateZ(0);
	}
     .uni-swiper-wrapper{
		 margin-right: 20rpx;
	 }
	.g-small {
		font-size: 24rpx;
		line-height: 44rpx;
	}

	/* #ifdef MP-WEIXIN */
	.swiper .wx-swiper-dot {
		width: 8rpx;
		height: 8rpx;
		display: inline-flex;
		background: none;
		justify-content: space-between;
	}

	.swiper .wx-swiper-dot::before {
		content: '';
		flex-grow: 1;
		background: rgba(255, 255, 255, 0.8);
		border-radius: 16rpx;
		overflow: hidden;
	}

	.swiper .wx-swiper-dot-active::before {
		background: #fff;
	}

	.swiper .wx-swiper-dot.wx-swiper-dot-active {
		width: 16rpx;
	}

	/* #endif */

	/* #ifndef MP-WEIXIN */
	>>>.swiper .uni-swiper-dot {
		width: 8rpx;
		height: 8rpx;
		display: inline-flex;
		background: none;
		justify-content: space-between;
	}

	>>>.swiper .uni-swiper-dot::before {
		content: '';
		flex-grow: 1;
		background: rgba(255, 255, 255, 0.8);
		border-radius: 16rpx;
		overflow: hidden;
	}

	>>>.swiper .uni-swiper-dot-active::before {
		background: #fff;
	}

	>>>.swiper .uni-swiper-dot.uni-swiper-dot-active {
		width: 16rpx;
	}

	/* #endif */

	.slide-image {
		width: 100%;
		height: 220rpx;
	}

	.class-box {
		/* padding-top: 30rpx; */
	}

	.class-item {
		background: #fff;
		/* width: 100%; */
		box-sizing: border-box;
		padding: 20rpx;
		margin-top: 20rpx;
		/* display: flex;
		justify-content: space-between; */
		margin-bottom: 20rpx;
		border-radius: 12rpx;
	}

	.class-name {
		font-size: 28rpx;
		margin-bottom: 10rpx;
		margin-top: 10rpx;
		font-weight: bold;
	}

	.g-container {
		padding-top: 20rpx;
		margin-right: 0rpx;
		display: flex;
		display: -webkit-flex;
		flex-direction: row;
		flex-wrap: wrap;
		
	}
	.g-containers{
		background:#fff;
		padding: 20rpx;
		margin:20rpx 0 !important;
		border-radius: 10rpx;
		display: flex;
		display: -webkit-flex;
		flex-direction: row;
		flex-wrap: wrap;
		
	}

	.g-introduction {
		font-size: 24rpx;
		color: #c6c6c6;
		height: 30rpx;
	}

	.g-box {
		text-align: center;
		margin-right: 16rpx;
		margin-bottom: 20rpx;
		/* padding-top: 40rpx; */
	}
	.g-box:nth-child(3n){
		text-align: center;
		margin-right: 0;
		/* padding-top: 40rpx; */
	}
	.g-container:nth-child(3n){
		width: 33%;
		text-align: center;
		margin-right: 0 !important;
		/* padding-top: 40rpx; */
	}
	.g-boxs {

		text-align: center;
		margin-right: 16rpx;
		margin-bottom: 20rpx;
		/* padding-top: 40rpx; */
		width:140rpx;
		
	}
	.g-boxs:nth-child(3n){
		text-align: center;
		margin-right: 0;
		/* padding-top: 40rpx; */
	}
	.g-containers:nth-child(3n){
		width: 33%;
		text-align: center;
		margin-right: 0 !important;
		/* padding-top: 40rpx; */
	}

	.tab-view {
		background: #ffffff;
	}

	.g-image {
		width: 122rpx;
		height: 122rpx;
		background:#fff;
		border-radius: 12rpx;
		padding:10rpx;
	}

	.g-span {
		width: 62%;
		position: relative;
	}

	.g-title {
		font-size: 24rpx;
		line-height: 30rpx;
		/* margin-bottom: 10rpx; */
		word-break: break-all;
		margin:0 auto;
		color:#616161;
	}
	.g-titles {
		font-size: 24rpx;
		line-height: 30rpx;
		/* margin-bottom: 10rpx; */
		overflow:hidden;
		/* width:100rpx; */
		height:28rpx;
		
		word-break: break-all;
		margin:0 auto;
		color:#616161;
	}

	.g-prices {
		font-weight: bold;
		display: flex;
	}

	.g-marketprice {
		color: #c8c8c8;
		font-size: 24rpx;
		text-decoration: line-through;
		margin-left: 10rpx;
		line-height: 40rpx;
	}

	.g-price {
		display: flex;
		margin-top: 20rpx;
		color: #E84842;
		font-size: 30rpx;
	}

	.g-tag {

		display: inline-block;
		border-radius: 6rpx;
		font-size: 20rpx;
		color: #E84842;
		margin-top: 4rpx;
	}

	.g-tags {
		border: 1rpx solid rgba(232, 72, 76, 0.5);
		display: inline-block;
		border-radius: 6rpx;
		color: #E84842;
		margin-top: 4rpx;
	}
</style>
